$(function() {
    var cols=6;
	var rows_delete=0;
	var mode="add";
	var m_id=0;
	var class_id=0;
    var url='controls/student_model.php';
 
	var objSelectTable=[];
	
	initialData();
	
	 function initialData(){
		 
		$.get(url, {"m":"class"},function(data){
			var cboObj=eval('('+data+')');	
			if (cboObj != null){
				$.each(cboObj, function(index, text) {
					$("#cboClass_Search").append(
						$('<option></option>').val(text).html(index)
					);
				});
			}
			$("#cboClass_Search").change();
		});
		 
		 $.get(url, {"m":"status"},function(data){
			var cboObj=eval('('+data+')');	
			if (cboObj != null){
				$.each(cboObj, function(index, text) {
					$("#cboStatus_Search").append(
						$('<option></option>').val(text).html(index)
					);
				});
			}
		});
		 $("#cboStatus_Search").append($('<option></option>').val(0).html(''));
		 
	}
	
	$("#cboClass_Search").change(function(){
		var cid=$(this).val();
		 
		$("#cboRoom_Search").find('option').remove();	
		
		$.get(url, {"m":"room","i":cid},function(data){
			var cboObj=eval('('+data+')');	
			if (cboObj != null){
				$.each(cboObj, function(index, text) {
					$("#cboRoom_Search").append(
						$('<option></option>').val(text).html(index)
					);
				});
			}
			SearchData();
		});
		
	});	
	$("#btnPrint").click(function(e){
		//$("#classname").val($("#cboClass_Search option:selected").text());
		//$("#frmStudent").submit();
		
		e.preventDefault(); 
		$("#classname").val($("#cboClass_Search option:selected").text());
		$("#frmStudent").get(0).setAttribute('action', 'rpt_student.php');
		$("#frmStudent").attr('target', '_blank').submit().removeAttr('target');
		
	});
	 function LoadData(year,co,name,cid,r,status){
        $( "#resultData tbody tr" ).remove();

        $('#resultData').dataTable( {
			"bProcessing": true,
			"bFilter": false,
			 "bPaginate": false,
			"bLengthChange": false,
			"sAjaxSource": url,
			"oLanguage": {
				"sEmptyTable":     "ไม่พบข้อมูล"
			},
			"bDestroy": true,
			"sServerMethod": "POST",
			"fnServerParams": function ( aoData ) {
					aoData.push( { "name": "m", "value": "load" },
								{"name":"cid","value":cid},
								{"name":"r","value":r},
								{"name":"co","value":co},
								{"name":"n","value":name},
								{"name":"s","value":status},
								{"name":"y","value":year}
							);
				},
			"aoColumnDefs":[
				{  "bVisible": false , "aTargets": [ 1 ] },	
				{   "bSortable": true, "aTargets": [ 2 ] ,"sClass":"left"},	
				{   "bSortable": true, "aTargets": [ 3 ] ,"sClass":"left" },	
				{   "bSortable": true, "aTargets": [ 4 ] ,"sClass":"left" },
				{   "bSortable": true, "aTargets": [ 5 ] ,"sClass":"center" },	
				{   "bSortable": true, "aTargets": [ 6 ] ,"sClass":"center" },	
				{   "bSortable": true, "aTargets": [ 7 ] ,"sClass":"center" },
				{ "aTargets": [ 8 ],
					"sClass":"center",
					"mData": null,
					"mRender": function ( data, type, full ) {
						 
						return "<a><img src='img/address_book_search_32.png' class='btnHistory' style='cursor:pointer;'/></a>";
						 
					}
				},
				{"aTargets": [ 9 ],
					"sClass":"center",
					"mData": null,
					"mRender": function ( data, type, full ) {
						 
						return "<a><img src='img/newspaper_info_32.png' class='btnInfo' style='cursor:pointer;'/></a>";
						 
					}
				}
			],"fnDrawCallback": function ( oSettings ) {
				 var that = this;
				/* Need to redo the counters if filtered or sorted */
                 var iTotalRecords = oSettings.fnRecordsTotal();
 
					if (iTotalRecords > 0){ 	
						if ( oSettings.bSorted || oSettings.bFiltered )
						{
							for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
							{
								$('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[i] ].nTr ).html( i+1 );
							}
						}
					}
					 
				} 
		 } );
		 
      }
	  
    $(".btnInfo").live('click',function(e){
		  e.preventDefault(); 
		var rows = $('#resultData').dataTable().fnGetPosition($(this).closest("tr").get(0));
		var data =$('#resultData').dataTable().fnGetData(rows);
		 
		m_id = data[1];
		
		$("#sid").val(m_id);
		$("#frmStudent").get(0).setAttribute('action', 'rpt_student_info.php');
		$("#frmStudent").attr('target', '_blank').submit().removeAttr('target');
	});
	
	$(".btnHistory").live('click',function(e){
	  e.preventDefault(); 
		var rows = $('#resultData').dataTable().fnGetPosition($(this).closest("tr").get(0));
		var data =$('#resultData').dataTable().fnGetData(rows);
		 
		m_id = data[1];
		 
		$("#sid").val(m_id);
		$("#frmStudent").get(0).setAttribute('action', 'rpt_student_history.php');
		$("#frmStudent").attr('target', '_blank').submit().removeAttr('target');
	});
	$("#btnSearch").click(function(){
		SearchData();
	});
	$.get(url, {"m":"year"},function(data){
			var cboObj=eval('('+data+')');	
			if (cboObj != null){
				$.each(cboObj, function(index, text) {
					$("#cboYear").append(
						$('<option></option>').val(text).html(text)
					);
				});
				$.get(url,{"m":"curryear"},function(data){
					$("#cboYear").val(data);
				});
			}
			
		});
	function SearchData(){
		var _code = $("#txtCode_Search").val();
		var _name = $("#txtStudent_Search").val();
		var _cid = $("#cboClass_Search").val();
		var _room = $("#cboRoom_Search").val();
		var _status = $("#cboStatus_Search").val();
		var _year = $("#cboYear").val();
		LoadData(_year,_code , _name, _cid, _room,_status );
	
	}
 
    
});


